import requests

# 1. 超时设置
# try:
#     response = requests.get("https://example.com", timeout=3)  # 3 秒超时
# except requests.exceptions.Timeout:
#     print("请求超时！")


# 2. 处理 Cookies
# response = requests.get("https://example.com")
# print(response.cookies)  # 获取 Cookies
# # 发送 Cookies
# cookies = {"session_id": "12345"}
# response = requests.get("https://example.com", cookies=cookies)
# print(response.status_code)


# 3. 会话保持（Session）
# 创建一个会话（自动保持 Cookies、连接池）
# with requests.Session() as session:
#     session.get("https://example.com/login", params={"user": "john"})
#     response = requests.get("https://example.com/dashboard")  # 保持登录状态


# 4. 文件上传
files = {"file": open("report.xlsx", "rb")}
response = requests.post("https://httpbin.org/post", files=files)